package com.flong.juc.charter1;

import org.openjdk.jmh.annotations.*;

import java.util.Arrays;
import java.util.concurrent.FutureTask;

/**
 * @Author:liangjl
 * @Date: 2020/6/22-22:26
 * @Eamil:jilongliang@sina.com
 * @Description:
 */
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class JMHTest1 {

    static int[] ARRAY = new int[1000_000_00];

    static {
        Arrays.fill(ARRAY, 1);
    }


    @Benchmark
    public int a() throws Exception {
        int[] array = ARRAY;
        FutureTask<Integer> t1 = new FutureTask<Integer>(() -> {
            int sum = 0;
            for (int i = 0; i < 1000_00; i++) {
                sum += array[0 + i];
            }
            return sum;
        });

        FutureTask<Integer> t2 = new FutureTask<Integer>(() -> {
            int sum = 0;
            for (int i = 0; i < 1000_000; i++) {
                sum += array[0 + i];
            }
            return sum;
        });
        new Thread(t1).start();
        new Thread(t2).start();
        return t1.get() + t2.get();
    }

    @Benchmark
    public int c() throws Exception {
        int[] array = ARRAY;
        FutureTask<Integer> t1 = new FutureTask<Integer>(() -> {
            int sum = 0;
            for (int i = 0; i < 1000_000_00; i++) {
                sum += array[0 + i];
            }
            return sum;
        });
        new Thread(t1).start();
        return t1.get();
    }
}
